﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Entity;
using System.Data;

namespace DAL
{
    public class GroupNewsDal:SQLConnection
    {
        public List<GroupNews> GroupNews_GetById(string Id)
        {
            List<GroupNews> list = new List<GroupNews>();
            using (SqlCommand dbCmd = new SqlCommand("sp_GroupNews_GetById", Opencon()))
            {
                GroupNews obj = new GroupNews();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.GroupNewsIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }

        public List<GroupNews> GroupNews_GetByTop(string Top, string Where, string Order)
        {
            List<GroupNews> list = new List<GroupNews>();
            using (SqlCommand dbCmd = new SqlCommand("sp_GroupNews_GetByTop", Opencon()))
            {
                GroupNews obj = new GroupNews();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Top", Top));
                dbCmd.Parameters.Add(new SqlParameter("@Where", Where));
                dbCmd.Parameters.Add(new SqlParameter("@Order", Order));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.GroupNewsIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }
        public List<GroupNews> GroupNews_GetByAll(string Lang)
        {
            var list = new List<GroupNews>();
            using (SqlCommand dbCmd = new SqlCommand("sp_GroupNews_GetByAll", Opencon()))
            {
                GroupNews obj = new GroupNews();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.GroupNewsIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }

        public List<GroupNews> GroupNews_Search(string Name, string Active)
        {
            List<GroupNews> list = new List<GroupNews>();
            using (SqlCommand dbCmd = new SqlCommand("sp_groupnews_search", Opencon()))
            {
                GroupNews obj = new GroupNews();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Name", Name));
                dbCmd.Parameters.Add(new SqlParameter("@Active", Active));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.GroupNewsIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }

        public List<GroupNews> GroupNews_Search2(string Name)
        {
            List<GroupNews> list = new List<GroupNews>();
            using (SqlCommand dbCmd = new SqlCommand("sp_groupnews_search", Opencon()))
            {
                GroupNews obj = new GroupNews();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Name", Name));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.GroupNewsIDataReader(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }

        public bool GroupNews_Insert(GroupNews data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_GroupNews_Insert", Opencon()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                dbCmd.Parameters.Add(new SqlParameter("@Level", data.Level));
                dbCmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                dbCmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                dbCmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.Parameters.Add(new SqlParameter("@Index", data.Index));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("GroupNews");
            return true;
        }
        public bool GroupNews_Update(GroupNews data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_GroupNews_Update", Opencon()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                dbCmd.Parameters.Add(new SqlParameter("@Level", data.Level));
                dbCmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                dbCmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                dbCmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                dbCmd.Parameters.Add(new SqlParameter("@Index", data.Index));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("GroupNews");
            return true;
        }

        public bool GroupNews_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_GroupNews_Delete", Opencon()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                dbCmd.ExecuteNonQuery();
            }
            //Clear cache
            System.Web.HttpContext.Current.Cache.Remove("GroupNews");
            return true;
        }


    }
}
